package rpc.netcon.jetty.server;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.util.thread.ExecutorThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by zhangyanfei on 2018/8/27.
 * jetty 服务
 *
 * @author zhangyanfei
 * @date 2018/08/27
 */
public class JettyServer {
    private Logger logger = LoggerFactory.getLogger(JettyServer.class);
    private Server server;
    private Thread thread;

    public void start(final int port, final String ip, final String appName) {
        thread = new Thread(() -> {
            // 非阻塞线程池
            server = new Server(new ExecutorThreadPool());
            // 连接器
            ServerConnector connector = new ServerConnector(server);
            connector.setHost(ip);
            connector.setPort(port);
            server.setConnectors(new Connector[]{connector});

            //set a handle
            HandlerCollection handlerc = new HandlerCollection();
            handlerc.setHandlers(new Handler[] {new JettyServerHandler()});
            server.setHandler(handlerc);
            try {
                server.start();
                logger.info(">>>>>>>>>>> xxl-job jetty server start success at port:{}.", port);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        });
        thread.setDaemon(true);
        thread.start();
    }

    public void distory() {
    }
}
